//-------------------------------------------------------------------------
// Copyright (c) 2000-2009 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author: lxt
//
//-------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//-------------------------------------------------------------------------

package com.ufinity.estimation.site;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;

import com.ufinity.estimation.persistence.domain.SystemSetting;
import com.ufinity.estimation.service.ClientTypeService;
import com.ufinity.estimation.service.ResourceService;
import com.ufinity.estimation.service.SystemSettingService;
import com.ufinity.estimation.service.UserTypeService;

/**
 * Description of the class
 * 
 * @author lxt
 * @version 1.0
 * @since 2011-4-12
 */

public class GlobalEnv implements InitializingBean, ServletContextAware {

    public static final String   USER_TYPES     = "userTypes";

    public static final String   ClIENT_TYPES   = "clientTypes";

    public static final String   SYSTEM_SETTING = "systemSetting";

    private ServletContext       sc;

    private UserTypeService      userTypeService;

    private ClientTypeService    clientTypeService;

    private SystemSettingService systemSettingService;

    private ResourceService      resourceService;

    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    public void setUserTypeService(UserTypeService userTypeService) {
        this.userTypeService = userTypeService;
    }

    public void setClientTypeService(ClientTypeService clientTypeService) {
        this.clientTypeService = clientTypeService;
    }

    public void setSystemSettingService(
            SystemSettingService systemSettingService) {
        this.systemSettingService = systemSettingService;
    }

    public void afterPropertiesSet() throws Exception {
        if (sc != null) {
            sc.setAttribute(USER_TYPES, userTypeService.findAll());
            sc.setAttribute(ClIENT_TYPES, clientTypeService.findAll());
            
            Map<String,String> systemSettingMap = new HashMap<String, String>();
            List<SystemSetting> systemSettingList = systemSettingService.findAll();
            if (systemSettingList != null) {
                for(SystemSetting ss:systemSettingList) {
                    systemSettingMap.put(ss.getKey(), ss.getValue());
                }
            }
            sc.setAttribute(SYSTEM_SETTING, systemSettingMap);
            
            sc.setAttribute("resources", resourceService.findAll());
        }
    }

    public void setServletContext(ServletContext servletContext) {
        sc = servletContext;
    }

}
